package cn.codesea.housework_user.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.codesea.housework_user.R;
import cn.codesea.housework_user.adapter.listener.OnOperationClickListener;
import cn.codesea.housework_user.adapter.listener.OnRecyclerItemClickListener;
import cn.codesea.housework_user.adapter.OrderRcvAdapter;
import cn.codesea.housework_user.adapter.listener.OnRecyclerItemClickOrderListener;
import cn.codesea.housework_user.entity.Order;
import cn.codesea.housework_user.net.ServiceGenerator;
import cn.codesea.housework_user.net.data.Result;
import cn.codesea.housework_user.net.service.OrderService;
import cn.codesea.housework_user.ui.component.EvaDialog;
import cn.codesea.housework_user.utils.DateUtil;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 人员推荐页
 */
public class OrderListActivity extends AppCompatActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    OrderRcvAdapter adapter;
    List<Order> orders = new ArrayList<>();
    private LinearLayoutManager mLinearLayoutManager;

    @BindView(R.id.search_iv)
    ImageView search_iv;
    @BindView(R.id.search_et)
    EditText search_et;

    int userid;
    int index;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_orderlist);
        ButterKnife.bind(this);
        init();
    }

    private void init(){
        initData();
        initView();
    }

    private void initData() {
        Intent data = getIntent();

        userid = data.getIntExtra("userid",-1);
        index = data.getIntExtra("index",0);

        updateRecyclerView();

    }

    private void initView(){

        // 初始化 toolbar
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        adapter = new OrderRcvAdapter(this,orders);
        adapter.setRecyclerItemClickListener(new OnRecyclerItemClickOrderListener() {
            @Override
            public void onItemClick(int Position, List<Order> list) {

            }
        });
        adapter.setOperationClickListener(new OnOperationClickListener(){
            @Override
            public void onOperationClick(int position, List<Order> list) {
                Order order = list.get(position);
                int status = order.getStatus();
                final Map params = new HashMap();
                params.put("orderid",order.getOrderid());
                switch (status){
                    case 0:
                        params.put("operate",0);// 取消
                        break;
                    case 2:
                        params.put("operate",2);// 结束
                        params.put("endtime",DateUtil.getDateNow());
                        break;
                    case 3:
                        params.put("operate",3);// 评价
                        EvaDialog evaDialog = new EvaDialog(OrderListActivity.this);
                        evaDialog.setSubmit(new EvaDialog.OnSubmitClickListener() {
                            @Override
                            public void onSubmitClick(String eva) {
                                params.put("evaluation",eva);
                                operate(params);
                            }
                        });
                        evaDialog.show();

                        return;
                    default:
                        break;
                }

                operate(params);

            }
        });
        mLinearLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mRecyclerView.setAdapter(adapter);
    }

    @OnClick(R.id.search_iv)
    public void search_iv(){
        Toast.makeText(OrderListActivity.this,search_et.getText(), Toast.LENGTH_SHORT).show();
    }

    /**
     *更新RecyclerView
     */
    private void updateRecyclerView(){
        OrderService service = ServiceGenerator.createService(OrderService.class);
        Map params = new HashMap();
        params.put("userid",userid);
        params.put("status",index);
        Call<Result> call = service.getOrders(params);
        call.enqueue(new Callback<Result>(){
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                resultProcess(response.body());
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {

            }
        });
    }

    /**
     * 结果处理
     */
    public void resultProcess(Result result){
        Type type = new TypeToken<List<Order>>(){}.getType();
        List<Order> orders = new Gson().fromJson(String.valueOf(result.getRes()),type);

        // 这里只展示一个
        if (orders.size() >0){
            this.orders.clear();
            this.orders.add(orders.get(0));
            adapter.notifyDataSetChanged();
        }

    }

    /**
     * 对订单的网络操作
     * @param params
     */
    private void operate(Map params){
        OrderService service = ServiceGenerator.createService(OrderService.class);
        Call<Result> call = service.operateOrder(params);
        call.enqueue(new Callback<Result>(){
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                operateResultProcess(response.body());
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {

            }
        });
    }

    /**
     * 操作（取消、结束、评价）结果处理
     */
    public void operateResultProcess(Result result){
        Toast.makeText(OrderListActivity.this,"操作成功！",Toast.LENGTH_SHORT).show();
        updateRecyclerView();
    }
}
